package com.lagou.rpc.client.protocol;

import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class RpcChannel {
    private static final ConcurrentMap<String, Channel> channelMap = new ConcurrentHashMap<>();
    public static Channel getOrAddChannel(Channel ch, String url) {
        if (ch == null) {
            return null;
        }
        Channel ret = channelMap.get(url);
        if (ret == null) {
            if (ch.isActive()) {
                ret = channelMap.putIfAbsent(url, ch);
            }
            if (ret == null) {
                ret = ch;
            }
        }
        return ret;
    }
}
